package com.kangye.learnstudent.service.impl;

import com.kangye.learnstudent.entity.*;
import com.kangye.learnstudent.mapper.InfomationMapper;
import com.kangye.learnstudent.service.InfomationService;
import com.kangye.learnstudent.util.RedisStorageUtil;
import com.kangye.learnstudent.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.List;

/**
 * @program: learndeduo
 * @description: 实现通知公告逻辑接口
 * @author: 索腾龙
 * @create: 2021-05-17 11:03
 **/
@Service
@Transactional
public class InfomationServiceImpl implements InfomationService {
    @Autowired
    private InfomationMapper infomationMapper;
    //注入redis工具类
    @Autowired
    private RedisStorageUtil redisStorageUtil;
    //通知公告
    @Override
    public List<Infomation> selinfomation() {
        List<Infomation> list = infomationMapper.selinfomation();
        return list;
    }
    //固定试卷
    @Override
    public List<StudentPaper> selFixedTestPaper() {
        List<StudentPaper> list = infomationMapper.selFixedTestPaper();
        return list;
    }
    //班级试卷
    @Override
    public List<StudentPaper> selClassTestPaper(String classesName) {
        List<StudentPaper> list = infomationMapper.selClassTestPaper(classesName);
        return list;
    }
    //头像
    @Override
    public List<User> selHeadPortrait(String userUserName) {


//        // 从缓存中获取信息
//        String key = "user-" + userUserName;
//        ValueOperations<String, User> operations = redisStorageUtil.opsForValue();
//
//        // 缓存存在
//        boolean hasKey = redisTemplate.hasKey(key);
//        if (hasKey) {
//            userInfo user = operations.get(key);
//
//            LOGGER.info("从缓存中获取了用户 >> " +"id: "+ user.getId()+", username: "+user.getUsername()+",password: "+user.getPassword());
//            return user;
//        }
//        userInfo user;
//        // 从 DB 中获取用户信息
//        user = userDao.finduserById(id);
//
//
//        // 插入缓存
//        if(user!=null){
//            operations.set(key, user, 30, TimeUnit.SECONDS);
//            LOGGER.info("用户插入缓存 >> " +"id: "+ user.getId()+", username: "+user.getUsername()+",password: "+user.getPassword());
//        }
//
//        return user;
//    }


        User redisUser = (User) redisStorageUtil.getKeyOne("user_userName");
//        String redisCode = (String) redisStorageUtil.getKeyOne("register"+ redisUser.getUserPhone());

//        boolean hasKey = redisStorageUtil.getKeyOne(userUserName);
//        if (hasKey) {
//            userInfo user = operations.get(key);
//
//            LOGGER.info("从缓存中获取了用户 >> " +"id: "+ user.getId()+", username: "+user.getUsername()+",password: "+user.getPassword());
//            return user;
//        }
//        User user;
//        System.out.println(redisCode);
        List<User> list = infomationMapper.selHeadPortrait(userUserName);
        return list;
    }

    @Override
    public List<QuestionBank> questionsPaper(String spName) {
        List<QuestionBank> list = infomationMapper.questionsPaper(spName);
        System.out.println(list);
        return list;
    }


}